using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;

namespace LS.GameKit
{
    public class MultipleCommandSender : CommandSender
    {
        public List<CommandType> commandTypes;
        public bool canSend = true;
        [SerializeField]
        private int m_Value = 0;

        public int Value
        {
            get
            {
                return m_Value;
            }
            set
            {
                SetValue(value);
            }
        }

        protected override bool CanSend()
        {
            if (!IsActive()) return false;
            return canSend;
        }

        protected override bool SendCommand(CommandReceiver receiver, params object[] values)
        {
            if (receiver == null) return false;
            receiver.ReceiveByObject(commandTypes[m_Value], values);
            return true;
        }

        protected override bool SendCommandByEventModule(string key, params object[] values)
        {
            if (string.IsNullOrEmpty(key)) return false;
            FireEvent(key, commandTypes[m_Value], values);
            return true;
        }

        protected override bool SendCommandByUnityEvent(UnityEvent<CommandType, object[]> unityEvent, params object[] values)
        {
            if (unityEvent == null) return false;
            unityEvent.Invoke(commandTypes[m_Value], values);
            return true;

        }

        public void SetValue(int index)
        {
            if (m_Value == index) return;
            m_Value = Mathf.Clamp(index,0, commandTypes.Count-1);
            Send();
        }

        public void SetValueWithoutNotify(int index)
        {
            if (m_Value == index) return;
            m_Value = Mathf.Clamp(index, 0, commandTypes.Count - 1);
        }
    }
}